package com.yfy.web.controller.client;

import com.yfy.model.ResponseData.ArticleResponseData;
import com.yfy.model.entity.User;
import com.yfy.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Author yfy2001
 * @date 2022/11/16 17:00
 */
@Controller
@RequestMapping("/register")
public class RegisterController {
    private static final Logger logger = LoggerFactory.getLogger(CommentController.class);
    @Autowired
    private UserService userService;

    /**
     * 跳转至注册界面
     * @return
     */
    @GetMapping(value = "/registerPage")
    public String register() {
        return "comm/register";
    }

    /**
     * 注册确认接口
     * @param user
     * @return
     */
    @PostMapping(value = "/registerConfirm")
    @ResponseBody
    public ArticleResponseData registerConfirm(User user){
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String pwd=user.getPassword();
        // 密码加密存储
        user.setPassword(encoder.encode(pwd));
        user.setCreator("System");
        user.setModifier("System");
        user.setGmtModified(new Date());
        user.setGmtCreated(new Date());
        user.setIsDeleted("N");
        user.setValid(1);
        try {
            userService.register(user);
            logger.info("注册成功");
            return ArticleResponseData.ok();
        } catch (Exception e) {
            logger.error("注册失败");
            return ArticleResponseData.fail();
        }
    }
}
